app.controller('favController', ['$scope', 'storageService',
    function ($scope, storageService) {
        var path = 'events';
        $scope.hasFavs = false;
        
        if(localStorage.getItem('favs')){
            checkFavs();
        }
        
        // Check for updates and update data when done
        storageService.dataUpdated(path).then(function(results){
            if(results.data !== localStorage.getItem(path + '_updated')){
                storageService.updateData(path).then(function(){
                    checkFavs();
                });
            } 
        });
        
        function checkFavs(){
            var favs = localStorage.getItem('favs').split(',');
            var events = storageService.getItem('events');
            var favorites = [];
            for(var e = 0; e < events.length; e++){
                for(var i=0; i<favs.length; i++){
                    if(events[e].event_id === favs[i]){
                        $scope.hasFavs = true;
                        favorites = favorites.concat(events[e]);
                    }
                }
                $scope.favorites = favorites;
            }
        }
    }
]);